package so.shanku.essential.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.ShopIntroductAdapter;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/25.
 * 店铺详情介绍
 */
public class ShopIntroductionActivity extends BaseUIActivity implements ShopIntroductAdapter.TelPhoneClick {
    /**
     * 微博
     */
    @ViewInject(id = R.id.iv_weibo, click = "iv_weibo_click")
    private ImageView iv_weibo;
    /**
     * 微信
     */
    @ViewInject(id = R.id.iv_weichat, click = "iv_weichat_click")
    private ImageView iv_weichat;
    /**
     * to头部图片
     */
    @ViewInject(id = R.id.iv_shopintroduct)
    private ImageView iv_shopintroduct;
    /**
     * 头部信息
     */
    @ViewInject(id = R.id.tv_shopintroduct)
    private TextView tv_shopintroduct;
    /**
     * 网址
     */
    @ViewInject(id = R.id.tv_internet, click = "tv_internet_click")
    private TextView tv_internet;
    /**
     * 获取更多品牌（新浪 微信图标）
     */
    @ViewInject(id = R.id.linear_more_msg)
    private LinearLayout linear_more_msg;
    /**
     * 店铺信息的listview
     */
    @ViewInject(id = R.id.listview_shopintroduction)
    private ListView listview_shopintroduction;
    private ShopIntroductAdapter shopIntroductAdapter;
    private String shopId;//店铺id
    private JsonMap<String, Object> jsonMap;//店铺介绍数据

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_introduction);
        shopId = getIntent().getStringExtra(ExtraKeys.Key_Msg1);
        getData_ShopIntroduct();
    }

    /**
     * 打开浏览器
     *
     * @param internetUrl
     */
    public void gotoInternet(String internetUrl) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri content_url = Uri.parse(internetUrl);
        intent.setData(content_url);
        startActivity(intent);
    }

    /**
     * 进入店铺网址
     *
     * @param view
     */
    public void tv_internet_click(View view) {
//		gotoInternet("http://www.baidu.com");

        gotoInternet(jsonMap.getStringNoNull("companyWebseit"));
    }

    /**
     * 进入店铺微博
     *
     * @param view
     */
    public void iv_weibo_click(View view) {
        gotoInternet(jsonMap.getStringNoNull("weiBo"));
    }

    /**
     * 进入店铺微信
     */
    public void iv_weichat_click(View view) {
        String weichat = jsonMap.getStringNoNull("weChat");
        if (!TextUtils.isEmpty(weichat)) {
            showPopView(weichat);
        }
    }

    public void getData_ShopIntroduct() {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("shopId", shopId);
        GetDataQueue queue = new GetDataQueue();
        queue.setCallBack(callBack);
        queue.setActionName(GetDataConfing.GetAction_GetShopDescribeInfo);
        queue.setWhat(GetDataConfing.what_GetShopDescribeInfo);
        queue.setParamsNoJson(params);
        getDataUtil.getData(queue);
    }


    /**
     * 获取服务器数据的返回信息
     */
    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {
        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isOkAndCodeIsNot1(ShopIntroductionActivity.this,
                        entity.getInfo())) {
                    if (entity.what == GetDataConfing.what_GetShopDescribeInfo) {
                        jsonMap = JsonParseHelper.getJsonMap_JsonMap(
                                entity.getInfo(), JsonKeys.Key_Info);
                        setIntroductData(jsonMap);

                    }
                }
            } else {
                ShowGetDataError.showNetError(ShopIntroductionActivity.this);
            }
            loadingToast.dismiss();
        }
    };

    public void setIntroductData(JsonMap data) {
        String brandLogo = data.getStringNoNull("shopLogo");

        ImageLoader.getInstance().displayImage(brandLogo, iv_shopintroduct, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());
        tv_shopintroduct.setText(data.getStringNoNull("shopDescri"));
        tv_internet.setText(data.getStringNoNull("companyWebseit"));
        shopIntroductAdapter = new ShopIntroductAdapter(this, data.getList_JsonMap("offlineStoreList"));
        listview_shopintroduction.setAdapter(shopIntroductAdapter);
        shopIntroductAdapter.setTelPhoneClick(this);

    }

    public PopupWindow popupWindow;
    private TextView close_tv;
    private ImageView weChatCode;

    private void showPopView(String weichatStr) {
        if (popupWindow == null) {
            View view = getLayoutInflater().inflate(R.layout.weichat_code_layout, null);
            popupWindow = new PopupWindow(view,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            popupWindow.setContentView(view);

            weChatCode = (ImageView) view.findViewById(R.id.weChat_code_iv);
            ImageLoader.getInstance().displayImage(weichatStr, weChatCode, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());
            popupWindow.setAnimationStyle(R.style.popupAnimation);

            close_tv = (TextView) view
                    .findViewById(R.id.close_tv);

            close_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAtLocation(iv_weibo, Gravity.NO_GRAVITY, 0, 0);

        } else if (!popupWindow.isShowing()) {
            popupWindow.showAtLocation(iv_weibo, Gravity.NO_GRAVITY, 0, 0);
        }
    }

    @Override
    public void telPhoneClick(String phone) {
        startTelPhone(phone);
    }
}